﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace DeployTool
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            DeployStepControlList = ToUserControlList();
            First = DeployStepControlList[0].Step;
            Last = DeployStepControlList[DeployStepControlList.Count - 1].Step;
            Current = DeployStepControlList[0];
            InitButton();
            InitUserControl(Current);
        }

        #region property
        /// <summary>
        /// 用户控件列表
        /// </summary>
        public List<IDeployStep> DeployStepControlList { get; }

        /// <summary>
        /// 首步序号
        /// </summary>
        public StepEnum First { get; }

        /// <summary>
        /// 结尾序号
        /// </summary>
        public StepEnum Last { get; }

        /// <summary>
        /// 当前对象
        /// </summary>
        public IDeployStep Current { get; set; }
        #endregion

        #region private methods
        /// <summary>
        /// 获取用户控件
        /// </summary>
        /// <returns></returns>
        private List<IDeployStep> ToUserControlList()
        {
            List<IDeployStep> list = new List<IDeployStep>();
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetExportedTypes().Where(m => typeof(IDeployStep).IsAssignableFrom(m) && m.IsPublic && m.IsClass && !m.IsAbstract && !m.IsInterface && !m.IsGenericType))
                {
                    IDeployStep step = Activator.CreateInstance(type) as IDeployStep;
                    if (step != null)
                    {
                        list.Add(step);
                    }
                }
            }

            if (!list.Any())
            {
                throw new Exception("不存在用户控件");
            }

            foreach (IDeployStep item in list)
            {
                if (list.Count(m => item.Step == m.Step) >= 2)
                {
                    throw new Exception("用户控件【Step】重复");
                }
            }

            return list.OrderBy(m => m.Step).ToList();
        }

        /// <summary>
        /// 初始化用户控件
        /// </summary>
        private void InitUserControl(IDeployStep deployStep)
        {
            deployStep.RefreshData();
            UserControl control = deployStep as UserControl;
            control.Dock = DockStyle.Fill;
            panel_top.Controls.Clear();
            panel_top.Controls.Add(control);
        }

        /// <summary>
        /// 初始化按钮
        /// </summary>
        private void InitButton()
        {
            btn_prev.Enabled = Current.Step != First;
            btn_next.Text = Current.Step == Last ? "完成" : "下一步";
        }

        private void btn_prev_Click(object sender, EventArgs e)
        {
            if (Current.Step != First)
            {
                Current = DeployStepControlList[DeployStepControlList.IndexOf(Current) - 1];
                InitUserControl(Current);
                InitButton();
            }
        }

        private void btn_next_Click(object sender, EventArgs e)
        {
            if (Current.Step == Last)
            {
                Environment.Exit(0);
                return;
            }

            if (!Current.CanNextStep())
            {
                MessageBox.Show("事务未全部处理，不可进入下一步", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Current = DeployStepControlList[DeployStepControlList.IndexOf(Current) + 1];
            InitUserControl(Current);
            InitButton();
        }
        #endregion
    }
}
